var api = {
  dateFormat: function(time, fmt) {
    if(!time) {
      return "";
    }
    var d = new Date(time);
    var o = {
      "M+": d.getMonth() + 1, //月份   
      "d+": d.getDate(), //日   
      "h+": d.getHours(), //小时   
      "m+": d.getMinutes(), //分   
      "s+": d.getSeconds(), //秒   
      "q+": Math.floor((d.getMonth() + 3) / 3), //季度   
      "S": d.getMilliseconds() //毫秒   
    };
    if(/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
    for(var k in o)
      if(new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  },
  vueDataFormat: function(arg) {
    return JSON.parse(JSON.stringify(arg));
  },
  getUrlParams: function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if(r) {
      return decodeURI(r[2]);
    } else {
      return null;
    }
  },
  dataUrl: process.env.NODE_ENV == 'production' ?"http://b2x.poolgpu.com:5905" :"http://localhost:5905",
  get: function(obj) {
    obj.type = "GET";
    this.ajax(obj);
  },
  post: function(obj) {
    obj.type = "POST";
    this.ajax(obj);
  },
  ajax: function(obj) {
    $.ajax({
      type: obj.type,
      url: this.dataUrl + obj.url,
      data: obj.data,
      dataType: "json",
      success: function(data) {
        if(typeof data == "string") {
          data = JSON.parse(data)
        }
        obj.callback(data)
      }
    });
  },
  getReadableHashRateString: function(hashrate, unit) {
    unit = unit || false
    hashrate = (hashrate * 2);
    if(hashrate < 1000000) {
      if (unit) {
        return(Math.round(hashrate / 1000) / 1000).toFixed(2) + ' Sol/s';
      } else {
        return(Math.round(hashrate / 1000) / 1000).toFixed(2);
      }
    }
    if (unit) {
      var byteUnits = [' Sol/s', ' KSol/s', ' MSol/s', ' GSol/s', ' TSol/s', ' PSol/s'];
      var i = Math.floor((Math.log(hashrate / 1000) / Math.log(1000)) - 1);
      hashrate = (hashrate / 1000) / Math.pow(1000, i + 1);
      return hashrate.toFixed(2) + byteUnits[i];
    } else {
      hashrate = (hashrate / 1000) / Math.pow(1000, 1);
      return hashrate.toFixed(2);
    }
  },
  unitCover: function(hashrate) {
    if(hashrate <= 0) {
      return hashrate
    }
    var i = 0;
    var units = ['H', 'KH', 'MH', 'GH', 'TH', 'PH'];
    while(hashrate > 1000) {
      hashrate = hashrate / 1000;
      i++;
    }
    return hashrate.toFixed(3) + ' ' + units[i];
  }
}
export default api;
